<template>
	<view class="page">
		<fu-custom :isBack="true" :isBottom="true" bgColor="bg-white">
			<view slot="content">{{i18n["设置"]}}</view>
		</fu-custom>
		<view class="fu-fs28">
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/setting/modify-password-verify-phone/index?type=3">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs28 text-333">修改手机号</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<view style="width: 100%;height: 20rpx;background-color: #F6F7F9;"></view>
			<!-- 语言切换 start -->
			<!-- <view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/setting/language/index">
      	<view class="flex align-center justify-between padding-tb solid-bottom">
      		<text class="fu-fs28 text-333">{{i18n['语言']}}</text>
      		<text class="cuIcon-right"></text>
      	</view>
      </view> -->
			<!-- 语言切换 end -->
			<!-- #ifdef APP-PLUS || H5 -->
				<!-- 个性化推送设置 start -->
				<!-- <view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/individuation/individuation">
					<view class="flex align-center justify-between padding-tb solid-bottom">
						<text class="fu-fs28 text-333">个性化推送设置</text>
						<text class="cuIcon-right"></text>
					</view>
				</view> -->
				<!-- 个性化推送设置 end -->
				<!-- 通知设置 start -->
				<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/newsInform/newsInform">
					<view class="flex align-center justify-between padding-tb solid-bottom">
						<text class="fu-fs28 text-333">通知设置</text>
						<text class="cuIcon-right"></text>
					</view>
				</view>
				<!-- 通知设置 end -->
				<!-- 权限管理 start -->
				<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/permissionSet/permissionSet">
					<view class="flex align-center justify-between padding-tb solid-bottom">
						<text class="fu-fs28 text-333">权限管理</text>
						<text class="cuIcon-right"></text>
					</view>
				</view>
				<!-- 权限管理 end -->
			<!-- #endif -->
			<!-- 用户协议 start -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/user/login/user-agreement/index?id=1" style="margin-top: 24rpx;">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs28 text-333">{{i18n['用户协议']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 用户协议 end -->
			<!-- 隐私协议 start -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/user/login/user-agreement/index?id=2">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs28 text-333">{{i18n['隐私政策']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 隐私协议 end -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/setting/helpheadback/helpheadback">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs28 text-333">帮助与反馈</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 关于我们 start -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/setting/about/index?id=6">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs28 text-333">{{i18n['关于我们']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 关于我们 end -->
			<!-- #ifdef APP-PLUS || H5 -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/setting/cancel-account/cancel-account/index">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs28 text-333">注销账号</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- #endif -->
			<!-- 联系我们 start TODO 原型上没有联系我们-->
			<!-- <view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/contactUs/index">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs28 text-333">联系我们</text>
					<text class="cuIcon-right"></text>
				</view>
			</view> -->
			<!-- 联系我们 end -->
			<!-- #ifdef APP-PLUS -->
			<!-- 清除缓存 start -->
			<view @tap="clear" class="margin-top-sm padding-lr bg-white">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs28 text-333">{{i18n['清除缓存']}}</text>
					<text>
						<text>{{fileSizeString}}</text>
						<text class="cuIcon-right"></text>
					</text>
				</view>
			</view>
			<!-- 清除缓存 end -->
			<!-- 系统版本 start -->
			<view @tap="handleTestingVersion" class="padding-lr bg-white padding-right">
				<view class="flex align-center justify-between padding-tb">
					<text class="fu-fs28 text-333">{{i18n['检查更新']}}</text>
					<text>
						<text>v{{version}}</text>
						<text class="cuIcon-right"></text>
					</text>
				</view>
			</view>
			<!-- 系统版本 end -->

			<!-- #endif -->
		</view>
		<!-- 退出登录按钮 end -->
		<!-- <view class="btn_cloce" v-if="isLogin">
			<view class="fu-fs28 text-center btn" @click="logout">{{'退出登录'}}</view>
		</view> -->
		<view style="margin-top:100rpx;width: 100%;display: flex;justify-content: center;">
			<view @click="logout" style="color: #FD9CAE;display:flex;justify-content:center;align-items:center;width: 690rpx;height: 88rpx;border-radius: 44rpx 44rpx 44rpx 44rpx;border: 1rpx solid #FD9CAE;">
				退出登录
			</view>
		</view>
		
		<!-- 退出登录按钮 end -->
		<!-- APP更新组件 start -->
		<app-update ref="update"></app-update>
		<!-- APP更新组件 end -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				version: '', // 版本号
				fileSizeString: '0B', // 缓存大小
				oldPhone: true, // 是否是老旧设备 true: 不支持指纹或人脸识别 false: 支持指纹或人脸识别
				themeColor:'#FF6F27'
			};
		},
		computed: {
			// 是否登录状态
			isLogin() {
				return this.$store.getters.isLogin;
			}
		},
		onLoad() {
			// #ifdef APP-PLUS
			// 获取App版本号
			plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
				this.version = widgetInfo.version;
			})
			this.formatSize();
			// #endif
		},
		methods: {
			// 更新检测
			handleTestingVersion() {
				this.$util.debounce(() => {
          this.$refs.update.checkAppUpdate();
        }, 1000, true)
			},
			// 退出登录
			logout() {
				this.$util.showModal({
					title:'提示',
					content: '确定退出当前账号？',
					confirmColor: "#FD9CAE",
					success: res => {
						if (res.confirm) {
							this.$store.commit('logout');
							uni.navigateBack();
							this.$store.dispatch('getCartList');
						}
					}
				});
			},
			
			//  #ifdef APP
			// 清除本地缓存
			clear() {
				this.$util.showModal({
					title: this.i18n['提示'],
					content: this.i18n['确定要清除本地缓存？'],
					confirmColor: this.themeColor,
					success: res => {
						if (res.confirm) {
							this.clearCache();
						}
					}
				})
			},
			
			// 格式化缓存大小
			formatSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = '0B';
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + 'B';
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + 'KB';
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + 'MB';
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + 'GB';
					}
				});
			},
				// 清除本地缓存
				clearCache() {
					let user = uni.getStorageSync('USER_INFO');
					uni.clearStorage();
					let that = this;
					let os = plus.os.name;
					if (os == 'Android') {
						let main = plus.android.runtimeMainActivity();
						let sdRoot = main.getCacheDir();
						let files = plus.android.invoke(sdRoot, 'listFiles');
						let len = files.length;
						if (len == 0) return that.$message.info(that.i18n['已经很干净了，没有需要清理的内容!']);
						for (let i = 0; i < len; i++) {
							let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径
							plus.io.resolveLocalFileSystemURL(
								filePath,
								function(entry) {
									if (entry.isDirectory) {
										entry.removeRecursively(
											function(entry) {
												//递归删除其下的所有文件及子目录
												uni.setStorageSync('USER_INFO', user);
												that.$message.info(that.i18n['缓存清理完成!']);
												that.formatSize(); // 重新计算缓存
											},
											function(e) {
												console.log(e.message);
											}
										);
									} else {
										entry.remove();
				
									}
								},
								function(e) {
									console.log('文件路径读取失败');
								}
							);
						}
					} else {
						// ios暂时未找到清理缓存的方法，以下是官方提供的方法，但是无效，会报错
						plus.cache.clear(function() {
							this.$message.info(this.i18n['缓存清理完成!']);
							that.formatSize();
						});
					}
				},
			// #endif
			
		}
	};
</script>

<style lang="scss" scoped>
	// 退出登录按钮 start
	.btn_cloce {
		height: 128rpx;
		.btn {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 40rpx;
			padding-bottom: 0;
			padding-bottom: constant(40rpx + safe-area-inset-bottom);
			padding-bottom: env(40rpx + safe-area-inset-bottom);
			margin: 0 auto;
			width: 686rpx;
			height: 88rpx;
			background-color: #fff;
			text-align: center;
			line-height: 88rpx;
			box-sizing: border-box;
			border: 1rpx solid #BFBFBF;
			border-radius: 50rpx;
		}
	}
	// 退出登录按钮 end
</style>
